#include <54func.h>
int main(int argc,char *argv[])
{
    // ./5_azhen 192.168.187,128 1234
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;//地址族为IPv4
    serverAddr.sin_port =htons(atoi(argv[2]));//将指定的端口先转为整数->转为网络字节序
    serverAddr.sin_addr.s_addr =inet_addr(argv[1]);//将点分十进制转换为网络地址
    int reuse =1;
    int ret = setsockopt(sockfd,SOL_SOCKET,
                         SO_REUSEADDR,&reuse,sizeof(reuse));
    ret = bind(sockfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(ret,-1,"bind");
    listen(sockfd,50);//此刻服务器被动，但是能够接收来自客户端的连接请求
    
    int netfd = -1;

    int epfd = epoll_create(1); //再内核中创建epoll文件对象，包含一个监听集合和一个就绪集合
   //设置监听集合 
    struct epoll_event event;//1、说明就绪的条件（读就绪还是写就绪）  2、就绪的行为（文件描述符）
    event.events = EPOLLIN;//读就绪
    event.data.fd = sockfd;//监听是否由新连接的接入
    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);

    char buf[4096] = {0};
    while(1)
    {
       struct epoll_event readySet[1024];//就绪集合,这里索性直接设置大一点
       int readyNum = epoll_wait(epfd,readySet,sizeof(readySet),-1);//-1表示无限等待
      

       for(int i = 0;i<readyNum;i++)
       {
           //如果当前有新连接加入
            if(readySet[i].data.fd == sockfd)
            {
                netfd = accept(sockfd,NULL,NULL);
                printf("aqiang has connect!,netfd = %d\n",netfd);
                epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);
                event.events = EPOLLIN;//读就绪
                event.data.fd = STDIN_FILENO;
                epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);
                event.events = EPOLLIN;//读就绪
                event.data.fd = netfd;
                epoll_ctl(epfd,EPOLL_CTL_ADD,netfd,&event);
            }else if(netfd != -1 &&readySet[i].data.fd == STDIN_FILENO)
            {
                bzero(buf,sizeof(buf));
                ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
                if(sret == 0)
                {
                    printf("azhen主动断开！");
                    epoll_ctl(epfd,EPOLL_CTL_DEL,STDIN_FILENO,NULL);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,netfd,NULL);
                    event.events = EPOLLIN;
                    event.data.fd = sockfd;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
                    close(netfd);
                    netfd =-1;
                    break;
                }
                send(netfd,buf,strlen(buf),0);

            }else if(netfd != -1 &&readySet[i].data.fd == netfd)
            {
                bzero(buf,sizeof(buf));
                ssize_t sret = recv(netfd,buf,sizeof(buf),0);
                if(sret == 0)
                {
                    printf("aqiang主动断开！\n");
                    epoll_ctl(epfd,EPOLL_CTL_DEL,STDIN_FILENO,NULL);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,netfd,NULL);
                    event.events = EPOLLIN;
                    event.data.fd = sockfd;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
                    close(netfd);
                    netfd =-1;
                    break;
                }
                printf("aqiang：%s\n",buf);
            }
       }

    }
    return 0;
}
